<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>汽贸城新增页面</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div>
			<form id="formSet1">
				<div class="fieldset1 tabel_col clearFix">
					<div class="caption">
						<span class="title">基本信息</span>
					</div>
					<div class="fieldBox col_33">
						<div class="label">名称</div>
						<div class="field">
							<input name="atNm" id="atNm" type="text" class="ui-textbox" validate="{required:true}"> 
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_66_auto">
						<div class="label">区域</div>
						<div class="field">
							<jsp:include page="../../system/sysRegion/region.jsp"
								flush="true">
								<jsp:param name="regionShowLvl" value="2" />
							</jsp:include>
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_100_auto">
						<div class="label">合同日期</div>
						<div class="field">
							<input name="beginDt" id="beginDt" type="text" class="ui-datepicker" value="" > 
								<span>--</span>
							<input name="endDt" id="endDt" type="text" class="ui-datepicker" value="" >
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">地址</div>
						<div class="field">
							<input name="atAddr" id="atAddr" type="text" class="ui-textbox" value="">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">资料上传</div>
						<div id="fileUpload" class="field">
							<jsp:include page="atcFilesUpload.jsp" flush="true">
								<jsp:param name="filePath" value="/atcBscInfo" />
							</jsp:include>
							<div id="fileUpload1" class="upfileBoxs"></div>
							<span class="sq-button sq-button-hasicon" id="btnUpload1" onclick="ajaxFileUploadImg('uploads1',1)">
								<i class="sqBtnIco_9"></i>上传
							</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div id="fileList">
							<jsp:include page="atcFileList.jsp" flush="true">
								<jsp:param name="pkId" value="${id }" />
								<jsp:param name="tabNm" value="ATC_BSC_INFO" />
							</jsp:include>
						</div>
					</div>
					
					<!-- 隐藏主键、有效期ID -->
	                <div class="fieldBox col_100" style="display:none;">
	                    <div class="field">
	                         <input name="pId" id="pId" type="text" class="ui-textbox" value="${id}">
	                         <!-- 原有效期-控制是否需要修改 -->
	                         <input name="beginDt_base" id="beginDt_base" type="text" class="ui-datepicker" value="">
							 <input name="endDt_base" id="endDt_base" type="text" class="ui-datepicker" value="">
							 <input name="isBegin" id="isBegin" type="text" class="ui-textbox" value="0">
							 <input name="isEnd" id="isEnd" type="text" class="ui-textbox" value="0">
	                    	 <!-- 上传文件使用：系统时间和当前登录人姓名 -->
	                         <input name="sysDate" id="sysDate" type="text" class="ui-textbox" value="${sysDate }">
	                         <input name="loginUsrNm" id="loginUsrNm" type="text" class="ui-textbox" value="${loginUsrNm }">
	                    </div>
	                </div> 
				</div>
				<div class="sqPanel">
					<div class="sqPanel_head inL">
						<div class="btnsBox inLC">
							<span id="spl_rl_add_btn" class="sq-button sq-button-round" onclick="splRlAddBtnEvent();">添加</span>
						</div>
						<span>主要人员信息</span>
					</div>
					<div class="sqPanel_body">
						<div id="ligergrid_A1" class="sq-grid"></div>
					</div>
				</div>
				<div id="historyList" class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">历史记录</span>
					</div>
					<div class="fieldBox col_100">
						<div id="ligergrid_file2" class="sq-grid"></div>
					</div>
				</div>
				<div class="btnsBox">
					<span id="saveBtn" class="sq-button sq-button-round" >保存</span>
					<span id="closeBtn" class="sq-button sq-button-round" onclick="removeNavTab()">关闭</span>
				</div>
			</form>
		</div>
	</div>
</body>
<script type="text/javascript">
var id = '${id}';
var socialGrid;
$(function(){
	initData();
	initSocialGrid();
	initForm(); 
	
	$("#saveBtn").click(function(event) {
		//汽贸城信息
		var form = liger.get("formSet1");
	    if (!form.valid()) {
	    	$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO , SQ_type: "warning" });
            return false; //验证不通过，不执行以下代码
        }
	  	//判断城市是否为空
		if(!getRgnProvCd()){
			$.ligerDialog.SQalert({content: "请选择所在省信息！",SQ_type: "warning"});
			return false;
		}
		if(!getRgnCityCd()){
			$.ligerDialog.SQalert({content: "请选择所在市信息！",SQ_type: "warning"});
			return false;
		}
		var isValid = SQ.validGrid('ligergrid_A1');
		if (isValid == false) {
	        $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO , SQ_type: "warning" });
	        return false;
	    }
		var isValid1 = liger.get('ligergrid2');
		var data = isValid1.getData();
	    if (data.length == 0) {
	        $.ligerDialog.SQalert({content: "请上传资料！" , SQ_type: "warning" });
	        return false;
	    }
		//判断合同有效期
		var beginDt = $("#beginDt").val();
		var endDt = $("#endDt").val();
		if(beginDt > endDt){
			$.ligerDialog.SQalert({content: "输入的合同有效期-终止日期应大于起始日期！",SQ_type: "warning"});
			return false;
		}
	    var data = form.getData();
	    data["rgnPrCd"] = getRgnProvCd();//获取省代码
	    data["rgnCyCd"] = getRgnCityCd();//获取市代码
	    
	    //获取附件上传的信息
	    var fileParams = getUploadResultParams();
	    data["fileParams"] = JSON.stringify(fileParams);
	    data["socialRlGridJSON"] = JSON.stringify(socialGrid.getData());
	    data["id"]=id;
	    var url = '<%=path%>/atc/atcBscInfo/add.do';
	    var wait_dialog;
	    $.ajax({
			type: 'post',
			dataType:'json',
			data:data,
			url: url,
			beforeSend: function(){
		          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		     },
			success:function(msg){
				 wait_dialog.close();
				 if(msg.success == 1){
					 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
					 refreshUlFileRecListByPkId(msg.data,'ATC_BSC_INFO');//刷新文件列表
					 clearUploadResultParams();//清除上传结果参数
					 socialGrid.reload();//刷新人员关系列表
					//回调刷新列表
			        var callback = GET_W2Wcallback();
			        callback();
			        
				    Remove_navTab();
			        
				 }else{
					 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
				 }
			}
		});
	});
	$("#formSet1").ligerForm({
        validate : true
	});
});


if(id){
	$("#ligergrid_file2").hide();
}
//初始化表单
function initForm(){
	//绑定change事件
	$("#startDt").ligerDateEditor({
		onChangeDate: function (value) {
			beginDtChange();
		}
	});
	$("#endDt").ligerDateEditor({
		onChangeDate: function (value) {
			endDtChange();
		}
	});
};
//判断有效期-开始时间
function beginDtChange() {
	var beginDt_val=$("#startDt").val();
	var beginDt_base=$("#beginDt_base").val();
	var endDt_base=$("#endDt_base").val();
	if(beginDt_val==beginDt_base){
		return false;
	}
	if(beginDt_val < endDt_base){
		$.ligerDialog.SQalert({content: "新有效期开始时间应大于原有效期结束时间！",SQ_type: "warning"});
		$("#startDt").val("");
		$("#isBegin").val("0");
	}else{
		$("#isBegin").val("1");
	}
}
//判断有效期-结束时间
function endDtChange() {
	var endDt_val=$("#endDt").val();
	var beginDt_val=$("#startDt").val();
	var endDt_base=$("#endDt_base").val();
	if(endDt_val==endDt_base){
		return false;
	}
	if(endDt_val < beginDt_val){
		$.ligerDialog.SQalert({content: "有效期结束时间应大于有效期开始时间！",SQ_type: "warning"});
		$("#endDt").val("");
		$("#isEnd").val("0");
	}else{
		$("#isEnd").val("1");
	}
}
function removeNavTab(){
	if(!id){
		//新增直接关闭
		Remove_navTab();
	}else{
		//修改验证资料和关系人员信息
		var isValid = SQ.validGrid('ligergrid_A1');
	    if (isValid == false) {
	        $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO , SQ_type: "warning" });
	        return false;
	    }
	    var isValid1 = liger.get('ligergrid2');
		var data = isValid1.getData();
	    if (data.length == 0) {
	        $.ligerDialog.SQalert({content: "请上传资料！" , SQ_type: "warning" });
	        return false;
	    }
	    Remove_navTab();
	}
}
 //资料上传
/*****************/
 //显示已上传但是未保存的文件列表
function showFiles(data){
	var sysDate = $("#sysDate").val();
	var loginUsrNm = $("#loginUsrNm").val();
	var f_Grid = liger.get("ligergrid2");
	var uploadJOSN = data;
    for (var i=0; i< uploadJOSN.length; i++){ 
        //添加行-追加行
        f_Grid.addRow({fileNm: uploadJOSN[i].fileNm, crtUsrRlNm: loginUsrNm, crtTm: sysDate, doSthFlag: "1"});
    }
}



/****************/
//修改初始化数据
function initData(){
	if(!id){
		return;
	}
	var _url = '<%=path%>/atc/atcBscInfo/getById.do';
		$.ajax({
			type : 'get',
			dataType : 'json',
			data : {
				id : id
			},
			url : _url,
			success : function(msg) {
				if (msg.success == 1) {
					setData(msg.data);//设置数据
				} else {
					$.ligerDialog.SQalert({
						content : msg.info,
						SQ_type : "error"
					});
				}
			}
		});
	}
	//设置页面数据
	function setData(data) {
		$("#atNm").val(data.atNm);
		$("#atAddr").val(data.atAddr);
		//日期处理
    	if(data.beginDt){
    		var date = new Date(data.beginDt);
    		var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
    		$("#beginDt").val(dateStr);
    		$("#beginDt_base").val(dateStr);
    	}
    	if(data.endDt){
    		var date = new Date(data.endDt);
    		var dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
    		$("#endDt").val(dateStr);
    		$("#endDt_base").val(dateStr);
    	}
		//区域数据处理
		setRgnProvCdUpdataValue(data.rgnPrCd);
		setRgnCityCdUpdataValue(data.rgnCyCd);
		setRegionSelectDataChange();
		//加载日期插件
		$("#startDt").trigger("change");
		$("#endDt").trigger("change") ;
	}
	//证件类型
    var certTypCdListJSON = [];
    <c:if test="${not empty certTypCdListJSON}"> certTypCdListJSON = ${certTypCdListJSON}; </c:if>
  	//联系人类型
    var rlTypCDListJSON = [];
    <c:if test="${not empty rlTypCDListJSON}"> rlTypCDListJSON = ${rlTypCDListJSON}; </c:if>
    //主要人员信息
    function initSocialGrid() {
		socialGrid = $("#ligergrid_A1").ligerGrid({
			url: '<%=path%>/atc/atcBscInfo/initAtcRlPsnList.do?',
	    	method : 'get',
	    	parms:{atcId:id},
	    	async: false,
	    	columns: [
	  	            {
	  	                display: '联系人姓名(<span class="c_red">*</span>)', name: 'rlNm', width: 150, editor: {type: 'text'},
	                    validate: function (value) {
	                        if (StringUtils.isEmpty(value)) return "此项为必填项！";
	                    }
					},
	  	            { display: '联系人类型(<span class="c_red">*</span>)', name: 'rlTypCd', width: 140,
						editor: {
		                       type: 'select',
		                       data: rlTypCDListJSON,
		                       valueField: 'key',
		                       textField: 'value'
		                },
						valueField: 'rlTypCd',
		                textField: 'rlTypCdNm',
						render: function (item) {
	                       return item.rlTypCdNm;
	                   },
						validate: function (value) {
						  if (StringUtils.isEmpty(value)) return "此项为必填项！";
						}
	  	            },
	  	            {
	  	               display: '证件类型(<span class="c_red">*</span>)', name: 'certTypCd', width: 140,
	   	  	           editor: {
	                       type: 'select',
	                       data: certTypCdListJSON,
	                       valueField: 'key',
	                       textField: 'value'
	                   },
	                   valueField: 'certTypCd',
	                   textField: 'certTypCdNm',
	                   render: function (item) {
	                       return item.certTypCdNm;
	                   },
	                    validate: function (value) {
	                        if (StringUtils.isEmpty(value)) return "此项为必填项！";
	                    }
	  	            },
	  	            {
	  	                display: '证件号(<span class="c_red">*</span>)', name: 'certNo', width: 150, editor: {type: 'text'},
	                    validate: function (value) {
	                        if (StringUtils.isEmpty(value)) return "此项为必填项！";
	                    }
	  	            },
	                {
	                    display: '联系电话(<span class="c_red">*</span>)', name: 'mp', width: 150, editor: {type: 'text'},
	                    validate: function (value) {
	                        if (StringUtils.isEmpty(value)) return "此项为必填项！";
	                    }
					},
	  	            { display: '备注', name: 'remark', minWidth: 150, editor: {type: 'text'}},
   	  	        	{ display: '操作', minWidth: 80 , align:'left',
		            	render: function (rowdata, rowindex, value){
		                	var d = "";
							if(rowdata.id){
								d='<a class="sq-button-hasicon" href="javascript:deleteDB(';
			            		d+="'"+rowdata.__id+"'";
			            		d+=",'"+rowdata.id+"'";
			                    d+=')"><i class="sqBtnIco_7"></i>删除</a>';
							}else{
								d='<a class="sq-button-hasicon" href="javascript:deletePage(';
			            		d+="'"+rowdata.__id+"'";
			                    d+=')"><i class="sqBtnIco_7"></i>删除</a>';
							}
		                    return d;
		            	}
		            }
	  	        ],
   	  	   usePager: false,
           root: 'rows',
           record: 'total',
           hideLoadButton: true,
           width: '100%',
           height: 300,
           heightDiff: -2,
           enabledEdit: true,
           isScroll: false,
           checkbox: false,
           rownumbers: true,
           allowUnSelectRow: true,
           alternatingRow: false,
           enabledSort:false
		});
	}
  //联系人新增
    function splRlAddBtnEvent(){
    	socialGrid.addRow({
    		rlNm: '',
    		rlTypCd: '',
    		certTypCd: '0',
    		certTypCdNm: '身份证',
    		certNo: '',
    		mp: '',
    		remark: ''
        });
    };
    function deletePage(rowdata_id){
    	$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
    		if(flag == false)return;
    		//删除页面
   			socialGrid.deleteRow(rowdata_id);
   			//socialGrid.reload();
    	})
    }
    function deleteDB(rowdata_id,_id){
    	//删除数据库
    	$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
    		if(flag == false)return;
    		var wait_dialog;
    		$.ajax({
    			type: 'get',
    			dataType:'json',
    			data:{rlId:_id,isDel:1},
    			url: '<%=path%>/atc/atcBscInfo/modifyRlPsn.do',
    			beforeSend: function(){
    			     wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING,SQ_type:"waitting"});
    			},
    			success:function(msg){
    				wait_dialog.close();
    				 if(msg.success == 1){
    					 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
    					 socialGrid.reload();
    				 }else{
    					 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
    				 }
    			}
    		});
    	});
    }
	
</script>

</html>